using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spinning.Spinning.Entity;
using Microsoft.Xna.Framework;
using Spinning.Spinning.Interface;
using Microsoft.Xna.Framework.Input.Touch;

namespace Spinning.Spinning.Scenes.ProcessScene
{
    class Item : AnimationSprite , ITouchable
    {
        Spinning.Unitity.Constants.ItemTypes ItemT;

        public delegate void TouchEventHandler(BaseEntity entity, TouchLocation touchLocation);
        public TouchEventHandler OnTouchEvent, OnReleaseEvent, OnHoldEvent;
        bool _pressed;

        public bool Enable { get; set; }

        public Item()
        {
            textureName = "role/item";
            Origin = new Vector2(.5f);
            ItemT = Spinning.Unitity.Constants.ItemTypes.Copter;
            Enable = true;
        }

        public override void OnLoad()
        {
            base.OnLoad();
            SetAnimation(new Point(1,5));
        }

        public void RemoreMyself(BaseEntity entity, TouchLocation touchLocation)
        {
            Game1.stageManager.GameSrate.ChooseItem.Remove(ItemT);
        }

        protected override void UpdateSelf(GameTime gameTime)
        {
            base.UpdateSelf(gameTime);
            if (Enable)
                Alpha = 1f;
            else
                Alpha = .5f;
        }

        public Spinning.Unitity.Constants.ItemTypes ItemType
        {
            get { return ItemT; }
            set
            {
                Visable = true;
                ItemT = value;
                int i = 0;
                switch (ItemT)
                {
                    case Unitity.Constants.ItemTypes.Copter:
                        i = 3;
                        break;
                    case Unitity.Constants.ItemTypes.Doll:
                        i = 1;
                        break;
                    case Unitity.Constants.ItemTypes.Marble:
                        i = 2;
                        break;
                    case Unitity.Constants.ItemTypes.Sandbag:
                        i = 0;
                        break;
                    case Unitity.Constants.ItemTypes.Windmill:
                        i=4;
                        break;
                }

                SetChangeFrameAnimation(new int[]{i});
                if (ItemT == Unitity.Constants.ItemTypes.Empty) Visable = false;

            }
        }

        public bool Contain(Microsoft.Xna.Framework.Vector2 position)
        {
            position = Vector2.Transform(position, UnProjection);
            Vector2 topLeft = Position - Origin * new Vector2(_frameSize.X, _frameSize.Y) * Scale;
            Vector2 rightDown = topLeft + new Vector2(_frameSize.X, _frameSize.Y) * Scale;
            return (position.X > topLeft.X && position.X < rightDown.X && position.Y > topLeft.Y && position.Y < rightDown.Y);
        }

        public void OnTouch(TouchLocation touchLocation)
        {
            _pressed = true;
            if (OnTouchEvent != null)
                OnTouchEvent(this, touchLocation);
        }

        public void OnRelease(TouchLocation touchLocation)
        {
            _pressed = false;
            if (OnReleaseEvent != null)
                OnReleaseEvent(this, touchLocation);
        }

        public void OnHold(TouchLocation touchLocation)
        {
            if (OnHoldEvent != null)
                OnHoldEvent(this, touchLocation);
        }

    }
}
